package com.nondo.dean.platform.service;


import com.nondo.dean.common.web.View;
import com.nondo.dean.platform.domain.Feedback;
import com.nondo.dean.platform.domain.FeedbackExample;
import com.nondo.dean.platform.mapper.FeedbackMapper;
import com.nondo.dean.platform.model.result.FeedBackResult;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.List;

/**
 * 意见反馈
 *
 * @author zhaoljin
 * @date 2018年4月3日
 */
@Component
public class FeedbackService {

    @Autowired
    private FeedbackMapper feedbackMapper;

    @Autowired
    private UserService userService;

    @Autowired
    RestTemplate restTemplate;

    /**
     * 插入意见反馈
     *
     * @param feedback
     * @return
     */
    @Transactional
    public int add(Feedback feedback) {
        return this.feedbackMapper.insertSelective(feedback);
    }


    /**
     * 查询总数
     * @return
     */
    public Long count(Feedback feedback){
        FeedbackExample example = new FeedbackExample();
        FeedbackExample.Criteria criteria = example.createCriteria();
        setCriteria(criteria, feedback);
        return feedbackMapper.countByExample(example);
    }

    /**
     * 意见反馈列表
     * @param offset
     * @param limit
     * @param feedback
     * @return
     */
    public List<FeedBackResult> getList(int offset,int limit,Feedback feedback){
        List<FeedBackResult> newList = new ArrayList<>();
        RowBounds rowBounds = new RowBounds(offset, limit);
        FeedbackExample example = new FeedbackExample();
        FeedbackExample.Criteria criteria= example.createCriteria();
        example.setOrderByClause("create_date DESC");
        setCriteria(criteria,feedback);
        List<Feedback> feedbackList = feedbackMapper.selectByExampleWithRowbounds(example,rowBounds);
        for(Feedback back : feedbackList){
            FeedBackResult resultBean = new FeedBackResult();
            resultBean.setUserName(userService.selectUser(back.getUserId()).getUserName());
            resultBean.setStoreName(storeName(back.getStoreId()));
            resultBean.setBody(back.getBody());
            newList.add(resultBean);
        }

        return newList;
    }

    /**
     * 返回店铺名称
     * @param storeId
     * @return
     */
    public String storeName(Integer storeId){
        String name = "";
        View<String> result = restTemplate.postForEntity("http://dean-dealer/dealer/v1/inner/store/select/"+storeId,null,View.class).getBody();
        if (result.getCode() == 20000 && result.getData() != null) {
            name = result.getData();
        }
        return name;
    }

    /**
     * 组装查询条件
     * @param criteria
     * @param
     */
    void setCriteria(FeedbackExample.Criteria criteria, Feedback feedback) {
        if (feedback != null) {
            if (feedback.getId() != null)
                criteria.andIdEqualTo(feedback.getId());
            if (feedback.getStoreId() != null)
                criteria.andStoreIdEqualTo(feedback.getStoreId());
        }
        criteria.andDelFlagEqualTo(Feedback.DEL_FLAG_NORMAL);
    }
}
